package com.smh.a06zipStreamDemo1;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipStreamDemo2 {
    public static void main(String[] args) throws IOException {

        //压缩一个文件

        File src = new File("day27-code\\file\\demo4\\a.txt");

        File dest = new File("day27-code\\file\\demo4");
        toZip(src, dest);
    }

    public static void toZip(File src, File dest) throws IOException {

        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(dest, "a.zip")));

        FileInputStream fis = new FileInputStream(src);

        //创建zipentry对象,将对象放入zos压缩流里面
        ZipEntry entry = new ZipEntry("a.txt");
        zos.putNextEntry(entry);

        //写入数据
        byte[] bytes = new byte[1024 * 1024];
        int len;
        while ((len = fis.read(bytes)) != -1) {
            zos.write(bytes, 0, len);
        }


        fis.close();
        zos.closeEntry();
        zos.close();
    }
}
